﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zatify.WebUI.Models.Account;
using Zatify.WebUI.Infrastructure;
using Zatify.WebUI.Infrastructure.Absrtact;
using Zatify.Domain.Abstract;
using Zatify.WebUI.Infrastructure.Concrete;
using Zatify.Domain.Data;
using Zatify.WebUI;

namespace Zatify.WebUI.Areas.Admin.Controllers
{
    //public class ZatifyIdentity : IIdentity
    //{
    //    public ZatifyIdentity(string firstName, string lastName)
    //    {
    //        this.Name = firstName;
    //        this.LastName = lastName;
    //    }

    //    public string AuthenticationType
    //    {
    //        get { return "Custom"; }
    //    }

    //    public bool IsAuthenticated
    //    {
    //        get { return (!String.IsNullOrEmpty(Name) && !String.IsNullOrEmpty(LastName)); }
    //    }

    //    public string Name
    //    {
    //        get;
    //        private set;
    //    }

    //    public string LastName
    //    {
    //        get;
    //        private set;
    //    }
    //}

    //public class ZatifyPrincipal : IPrincipal
    //{
    //    public ZatifyPrincipal(ZatifyIdentity identity)
    //    {
    //        this.Identity = identity;
    //    }

    //    public IIdentity Identity
    //    {
    //        get;
    //        private set;
    //    }

    //    public bool IsInRole(string role)
    //    {
    //        return true;
    //    }
    //}

    [Utils.MyRequireHttps]
    public class AccountController : AdminBaseController
    {
        private IZatifyRepository repository;
        private IZatifyAuthProvider authProvider;
        private IPasswordRecoveryService passwordRecoveryService;

        protected override AdminModules AdminModule
        {
            get
            {
                return AdminModules.Account;
            }
        }

        public AccountController(IZatifyRepository repository, IZatifyAuthProvider authProvider, IPasswordRecoveryService passwordRecoveryService)
        {
            this.repository = repository;
            this.authProvider = authProvider;
            this.passwordRecoveryService = passwordRecoveryService;
        }

        [HttpGet]
        public ViewResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(AdminLoginViewModel model, string returnUrl)
        {
            if (this.ModelState.IsValid)
            {
                //if (this.authProvider.AuthenticateUser(model.UserName, model.Password, true, model.RememberMe))
                if (this.authProvider.AuthenticateUser(new SystemUser() { Login = model.UserName, Password = model.Password, RememberUser = model.RememberMe, TimeZoneOffset = model.TimeZoneOffset }))
                {
                    var redirectUrl = (!String.IsNullOrWhiteSpace(returnUrl) ? returnUrl : Url.Action("List", "Users"));

                    return Redirect(redirectUrl);
                }
                else
                {
                    this.ModelState.AddModelError("", "Incorrect user or password");
                    return View(model);
                }
            }
            else
            {
                return View();
            }
        }

        [HttpGet]
        public ViewResult PasswordRecovery()
        {
            return View();
        }

        [HttpPost]
        public ViewResult PasswordRecovery(string email, string phone)
        {
            //var userEmailExists = repository.Users.FirstOrDefault(u => u.Email == email) != null;
            var systemUserEmailExists = repository.SystemUsers.FirstOrDefault(u => u.Email == email) != null;

            //var userPhoneExists = repository.Users.FirstOrDefault(u => u.Phone == phone) != null;
            var systemUserPhoneExists = repository.SystemUsers.FirstOrDefault(u => u.Phone == phone) != null;

            var password = String.Empty;

            if (String.IsNullOrWhiteSpace(email) && String.IsNullOrWhiteSpace(phone))
            {
                this.ModelState.AddModelError("email_and_phone_empty_error", "Enter Email or Phone where to sent new password");
            }
            else if (!String.IsNullOrWhiteSpace(email) && (/*!userEmailExists &&*/ !systemUserEmailExists))
            {
                this.ModelState.AddModelError("Email", "Incorrect Email entered");
            }
            else if (!String.IsNullOrWhiteSpace(phone) && (/*!userPhoneExists &&*/ !systemUserPhoneExists))
            {
                this.ModelState.AddModelError("Phone", "Incorrect Phone entered");
            }

            if (this.ModelState.IsValid)
            {
                SendPasswordRecovery(email, phone);

                return View("PasswordSended");
            }
            else
            {
                return View();
            }
        }

        public ActionResult LogOff()
        {
            this.authProvider.LogOff();
            //this.ZatifyUser = null;
            TempData["LogOut_Message"] = "You logged out";

            return RedirectToAction("Login");
        }

        private void SendPasswordRecovery(string email, string phone)
        {
            if (!String.IsNullOrWhiteSpace(phone))
            {
                //var password = repository.Users.FirstOrDefault(u => u.Phone == phone) != null ? repository.Users.FirstOrDefault(u => u.Phone == phone).Password : String.Empty;
                //if (String.IsNullOrWhiteSpace(password))
                //    password = repository.SystemUsers.FirstOrDefault(u => u.Phone == phone) != null ? repository.SystemUsers.FirstOrDefault(u => u.Phone == phone).Password : String.Empty;

                var password = repository.SystemUsers.FirstOrDefault(u => u.Phone == phone) != null ? repository.SystemUsers.FirstOrDefault(u => u.Phone == phone).Password : String.Empty;

                passwordRecoveryService.SendPasswordRecoveryToMobile(password, phone);
            }

            if (!String.IsNullOrWhiteSpace(email))
            {
                //var password = repository.Users.FirstOrDefault(u => u.Email == email) != null ? repository.Users.FirstOrDefault(u => u.Email == email).Password : String.Empty;
                //if (String.IsNullOrWhiteSpace(password))
                //    password = repository.SystemUsers.FirstOrDefault(u => u.Email == email) != null ? repository.SystemUsers.FirstOrDefault(u => u.Email == email).Password : String.Empty;

                var password = repository.SystemUsers.FirstOrDefault(u => u.Email == email) != null ? repository.SystemUsers.FirstOrDefault(u => u.Email == email).Password : String.Empty;

                passwordRecoveryService.SendPasswordRecoveryToEmail(password, email);
            }
        }
    }
}
